package com.suray.wcs.web.system.controller;


import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.SecurityUtils;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.init.SystemInit;
import com.suray.wcs.service.system.entity.WmsUrlDB;
import com.suray.wcs.service.system.service.WmsUrlDBService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wmsUrl")
@CrossOrigin(value = "*", allowCredentials = "true")
public class WmsUrlDBController extends BaseController {
    @Autowired
    private WmsUrlDBService wmsUrlDBService;

    /**
     * 分页查询
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wmsUrl:query')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public TableDataInfo getPortList(WmsUrlDB wmsUrlDB) {
        startPage();
        List<WmsUrlDB> result = wmsUrlDBService.selectList(wmsUrlDB);
        return getDataTable(result);
    }

    @PreAuthorize("@ss.hasPermi('wmsUrl:query')")
    @ApiOperation(value = "获取接口路径信息")
    @GetMapping(value = "/getWmsUrl/{urlId}")
    public ResponseData getWmsUrl(@PathVariable Integer urlId) {
        return ResponseData.success(wmsUrlDBService.getById(urlId));
    }

    /**
     * 新增wms接口路径
     *
     * @param wmsUrlDB
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wmsUrl:add')")
    @ApiOperation(value = "新增wms接口路径")
    @Log(title = "库口管理", method = "新增wms接口路径", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addWmsUrl")
    public ResponseData addWmsUrl(WmsUrlDB wmsUrlDB) {
        if (wmsUrlDBService.checkInterfaceNameUnique(wmsUrlDB)) {
            return ResponseData.error("新增wms接口路径'" + wmsUrlDB.getInterfaceName() + "'失败，接口已存在");
        }
        wmsUrlDB.setCreateBy(SecurityUtils.getUsername());
        wmsUrlDB.setCreateTime(new Date());
        boolean result = wmsUrlDBService.save(wmsUrlDB);
        if (result) {
            SystemInit.URLS.put(wmsUrlDB.getInterfaceName(),wmsUrlDB.getUrlAddr());
        }
        return toAjax(result);
    }

    /**
     * 修改wms接口路径
     *
     * @param wmsUrlDB
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wmsUrl:edit')")
    @ApiOperation(value = "修改wms接口路径")
    @Log(title = "库口管理", method = "修改wms接口路径", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/updateWmsUrl")
    public ResponseData updateWmsUrl(WmsUrlDB wmsUrlDB) {
        if (wmsUrlDBService.checkInterfaceNameUnique(wmsUrlDB)) {
            return ResponseData.error("修改wms接口路径'" + wmsUrlDB.getInterfaceName() + "'失败，接口已存在");
        }
        boolean result = wmsUrlDBService.updateById(wmsUrlDB);
        if (result) {
            SystemInit.URLS.put(wmsUrlDB.getInterfaceName(),wmsUrlDB.getUrlAddr());
        }
        return toAjax(result);
    }


    /**
     * 删除wms接口路径
     *
     * @param urlIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('wmsUrl:delete')")
    @ApiOperation(value = "删除wms接口路径")
    @Log(title = "库口管理", method = "删除wms接口路径", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/delWmsUrl/{urlIds}")
    public ResponseData delWmsUrl(@PathVariable List<Integer> urlIds) {
        return toAjax(wmsUrlDBService.removeByIds(urlIds));
    }
}
